package org.dbstar.scheduler.concurrent;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.dbstar.scheduler.beans.Job;
import org.dbstar.scheduler.job.JobRunner;


/**
 * 封装适应于执行Job的ThreadPoolExecutor
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-4-6
 */
public final class JobThreadPoolExecutor extends ThreadPoolExecutor {
	/**
	 * @param corePoolSize
	 * @param maximumPoolSize
	 * @param keepAliveTime
	 * @param unit
	 * @param workQueue
	 */
	public JobThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
			BlockingQueue<Runnable> workQueue) {
		super(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, workQueue);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
		if (JobRunner.class.isInstance(callable)) {
			return (RunnableFuture<T>) newTaskFor((JobRunner) callable);
		} else {
			return super.newTaskFor(callable);
		}
	}

	private RunnableFuture<Job> newTaskFor(JobRunner runner) {
		return new JobFutureTask(runner);
	}
}
